package com.q.tqb.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.reflect.TypeToken;
import com.q.tqb.R;
import com.q.tqb.activity.grab.GrabSpeaking2Activity;
import com.q.tqb.adapter.GrabRedEnvelopeAdapter;
import com.q.tqb.base.AppConst;
import com.q.tqb.model.RedListData;
import com.q.tqb.util.LogUtil;
import com.q.tqb.util.http.IResponse;
import com.q.tqb.util.http.OkHttpUtil;
import com.q.tqb.view.refresh.GRecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class GrabListFragment extends Fragment {

    Unbinder unbinder;
    @BindView(R.id.rv_grab)
    GRecyclerView mRvGrab;
    private GrabRedEnvelopeAdapter mGrabRedEnvelopeAdapter;
    private ArrayList<RedListData> mRedListData;

    private int currentPageNo;
    private int type;

    public GrabListFragment() {
        // Required empty public constructor
    }

    public static GrabListFragment newInstance(int type) {
        GrabListFragment fragment = new GrabListFragment();
        Bundle args = new Bundle();
        args.putInt("type", type);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            type = getArguments().getInt("type");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_grab_list, container, false);
        unbinder = ButterKnife.bind(this, view);
        init();
        getListData();
        return view;
    }

    private void init() {
        mRedListData = new ArrayList<>();

        mRvGrab.setLoadingMoreEnabled(true);
        mRvGrab.setLoadingListener(new GRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                currentPageNo = 1;
                mRvGrab.setNoMore(false);
                mRvGrab.setLoadingMoreEnabled(true);
                getListData();
            }

            @Override
            public void onLoadMore() {
                currentPageNo++;
                getListData();
            }
        });

        mGrabRedEnvelopeAdapter = new GrabRedEnvelopeAdapter(getActivity(), mRedListData);
        mGrabRedEnvelopeAdapter.setOnItemClickListener(new GrabRedEnvelopeAdapter.OnItemClickListener() {
            @Override
            public void onClick(int position) {
                Intent intent;
                if (mRedListData.get(position).getPacket_type() == 1) {
                    intent = new Intent(getActivity(), GrabSpeaking2Activity.class);
                    intent.putExtra("rid", mRedListData.get(position).getRid());
                    intent.putExtra("uid", mRedListData.get(position).getUid());
                    startActivity(intent);
                } else if (mRedListData.get(position).getPacket_type() == 2) {
                    // intent = new Intent(getActivity(), GrabImageActivity.class);
                    intent = new Intent(getActivity(), GrabSpeaking2Activity.class);
                    intent.putExtra("rid", mRedListData.get(position).getRid());
                    intent.putExtra("uid", mRedListData.get(position).getUid());
                    startActivity(intent);
                }else if (mRedListData.get(position).getPacket_type() == 3) {
                    intent = new Intent(getActivity(), GrabSpeaking2Activity.class);
                    intent.putExtra("rid", mRedListData.get(position).getRid());
                    intent.putExtra("uid", mRedListData.get(position).getUid());
                    intent.putExtra("type", 1);
                    startActivity(intent);
                }
            }
        });

        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 2);
        mRvGrab.setLayoutManager(gridLayoutManager);
        mRvGrab.setAdapter(mGrabRedEnvelopeAdapter);
        mRvGrab.setRefresh();
    }


    private void getListData() {
        Map<String, String> map = new HashMap<>();
        map.put("pageNo", currentPageNo + "");
        map.put("pageSize ", 10 + "");
        map.put("packet_type", type + "");

        OkHttpUtil.getInstance().post(getActivity(), AppConst.URL_GET_PACKET_LIST, map, new IResponse() {
            @Override
            public void onSuccess(JsonElement element) {
                LogUtil.i(element.toString());
                Gson gson = new Gson();
                String result = element.toString().replace("null", "\"\"");
                ArrayList<RedListData> redListData = gson.fromJson(result, new TypeToken<ArrayList<RedListData>>() {
                }.getType());
                if (currentPageNo == 1) {
                    mRedListData.clear();
                    mRvGrab.refreshComplete();
                    if (redListData.size() < 10) {
                        mRvGrab.setNoMore(true);
                    }
                } else {
                    if (redListData.size() < 10) {
                        mRvGrab.setNoMore(true);
                    } else {
                        mRvGrab.loadMoreComplete();
                    }
                }
                mRedListData.addAll(redListData);
                mGrabRedEnvelopeAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(int statusCode, String errorMsg) {
                if(mRvGrab==null)
                    return;
                if (currentPageNo == 1) {
                    mRvGrab.refreshComplete();
                } else {
                    mRvGrab.loadMoreComplete();
                }
                LogUtil.i(errorMsg + "statusCode" + statusCode);
                // Toast.makeText(getActivity(), errorMsg, Toast.LENGTH_SHORT).show();
            }
        });
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
